



#include "app_keyboard_server.h"
#include "one_wire_bus_app.h"
#include "app_parameter.h"

uint8_t handshake=0;
uint16_t time=0;
uint8_t syn_flag=0,device_flag=0;
uint8_t device_info_send_flag=0;

uint8_t device_version_info_send_flag=0; //设备版本信息发送标志
uint8_t device_extend_info_send_flag=0;  //设备扩展信息发送标志
uint8_t accel_info_send_flag=0;  //accel信息发送标准

const uint8_t my_PnPtrs [] = {0xC6, 0x09, 0x15, 0x05};//yongle    vid 2个字节   pid  2个字节
//const uint8_t my_PnPtrs [] = {0xC6, 0x09, 0x011, 0x05};//changan    vid 2个字节   pid  2个字节

const  uint8_t  hid_reportMap_aaa[] =
{
	//keyboard report in
	0x05, 0x01,     // Usage Pg (Generic Desktop)
	0x09, 0x06,     // Usage (Keyboard)
	0xA1, 0x01,     // Collection: (Application)
	0x85, HID_REPORT_ID_KEYBOARD_INPUT,     // Report Id (keyboard)
				  //
	0x05, 0x07,     // Usage Pg (Key Codes)
	0x19, 0xE0,     // Usage Min (224)  VK_CTRL:0xe0
	0x29, 0xE7,     // Usage Max (231)  VK_RWIN:0xe7
	0x15, 0x00,     // Log Min (0)
	0x25, 0x01,     // Log Max (1)
				  //
				  // Modifier byte
	0x75, 0x01,     // Report Size (1)   1 bit * 8
	0x95, 0x08,     // Report Count (8)
	0x81, 0x02,     // Input: (Data, Variable, Absolute)
				  //
				  // Reserved byte
	0x95, 0x01,     // Report Count (1)
	0x75, 0x08,     // Report Size (8)
	0x81, 0x01,     // Input: (static constant)

	//keyboard output
	//5 bit led ctrl: NumLock CapsLock ScrollLock Compose kana
	0x95, 0x02,    //Report Count (2)
	0x75, 0x01,    //Report Size (1)
	0x05, 0x08,    //Usage Pg (LEDs )
	0x09, 0x02,    //LED caps
	0x09, 0x09,    //LED mic
	0x91, 0x02,    //Output (Data, Variable, Absolute)
	//3 bit reserved
	0x95, 0x01,    //Report Count (1)
	0x75, 0x06,    //Report Size (6)
	0x91, 0x01,    //Output (static constant)

	// Key arrays (6 bytes)
	0x95, 0x06,     // Report Count (6)
	0x75, 0x08,     // Report Size (8)
	0x15, 0x00,     // Log Min (0)
	0x25, 0xF7,     // Log Max (247)
	0x05, 0x07,     // Usage Pg (Key Codes)
	0x19, 0x00,     // Usage Min (0)
	0x29, 0xF7,     // Usage Max (247)
	0x81, 0x00,     // Input: (Data, Array)

	0xC0,            // End Collection

	//consumer report in
	0x05, 0x0C,   // Usage Page (Consumer)
	0x09, 0x01,   // Usage (Consumer Control)
	0xA1, 0x01,   // Collection (Application)
	0x85, HID_REPORT_ID_CONSUME_CONTROL_INPUT,   //     Report Id
	0x75,0x10,     //global, report size 16 bits
	0x95,0x01,     //global, report count 1
	0x15,0x01,     //global, min  0x01
	0x26,0xCA,0x02,  //global, max  0x2CA
	0x19,0x01,     //local, min   0x01
	0x2a,0xCA,0x02,  //local, max    0x2CA
	0x81,0x00,     //main,  input data varible, absolute
	0xc0,        //main, end collection
	
#if 0
	  0x05, 0x0D,        // Usage Page (Digitizer)
	  0x09, 0x04,        // Usage (Touch Pad)
	  0xA1, 0x01,        // Collection (Application)
	  0x85, HID_REPORT_ID_MOUSE_INPUT,        //   Report ID (3)

	  0x05, 0x0D,        //   Usage Page (Digitizer)
	  0x09, 0x22,        //   Usage (Finger)
	  0xA1, 0x02,        //   Collection (Logical)
	  0x09, 0x47,        //     Usage (0x47)
	  0x09, 0x42,        //     Usage (Tip Switch)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x25, 0x01,        //     Logical Maximum (1)
	  0x75, 0x01,        //     Report Size (1)
	  0x95, 0x02,        //     Report Count (2)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x51,        //     Usage (0x51)
	  0x25, 0x0F,        //     Logical Maximum (15)
	  0x75, 0x06,        //     Report Size (6)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
	  0x09, 0x30,        //     Usage (X)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x26, U16_LO(TOUCHPAD_X_SIZE), U16_HI(TOUCHPAD_X_SIZE),  //     Logical Maximum (3684)
	  0x35, 0x00,        //     Physical Minimum (0)
	  0x46, U16_LO(PHY_X), U16_HI(PHY_X),  //     Physical Maximum (401)
	  0x55, 0x0E,        //     Unit Exponent (-3)
	  0x65, 0x13,        //     Unit (System: SI Linear, Length: Centimeter)
	  0x75, 0x10,        //     Report Size (16)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x31,        //     Usage (Y)
	  0x26, U16_LO(TOUCHPAD_Y_SIZE), U16_HI(TOUCHPAD_Y_SIZE),  //     Logical Maximum (2176)
	  0x46, U16_LO(PHY_Y), U16_HI(PHY_Y),  //     Physical Maximum (275)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0xC0,              //   End Collection

	  0x05, 0x0D,        //   Usage Page (Digitizer)
	  0x09, 0x22,        //   Usage (Finger)
	  0xA1, 0x02,        //   Collection (Logical)
	  0x09, 0x47,        //     Usage (0x47)
	  0x09, 0x42,        //     Usage (Tip Switch)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x25, 0x01,        //     Logical Maximum (1)
	  0x75, 0x01,        //     Report Size (1)
	  0x95, 0x02,        //     Report Count (2)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x51,        //     Usage (0x51)
	  0x25, 0x0F,        //     Logical Maximum (15)
	  0x75, 0x06,        //     Report Size (6)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
	  0x09, 0x30,        //     Usage (X)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x26, U16_LO(TOUCHPAD_X_SIZE), U16_HI(TOUCHPAD_X_SIZE),  //     Logical Maximum (3684)
	  0x35, 0x00,        //     Physical Minimum (0)
	 0x46, U16_LO(PHY_X), U16_HI(PHY_X),  //     Physical Maximum (401)
	  0x55, 0x0E,        //     Unit Exponent (-3)
	  0x65, 0x13,        //     Unit (System: SI Linear, Length: Centimeter)
	  0x75, 0x10,        //     Report Size (16)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x31,        //     Usage (Y)
	  0x26, U16_LO(TOUCHPAD_Y_SIZE), U16_HI(TOUCHPAD_Y_SIZE),  //     Logical Maximum (2176)
	  0x46, U16_LO(PHY_Y), U16_HI(PHY_Y),  //     Physical Maximum (275)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0xC0,              //   End Collection

	  0x05, 0x0D,        //   Usage Page (Digitizer)
	  0x09, 0x22,        //   Usage (Finger)
	  0xA1, 0x02,        //   Collection (Logical)
	  0x09, 0x47,        //     Usage (0x47)
	  0x09, 0x42,        //     Usage (Tip Switch)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x25, 0x01,        //     Logical Maximum (1)
	  0x75, 0x01,        //     Report Size (1)
	  0x95, 0x02,        //     Report Count (2)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x51,        //     Usage (0x51)
	  0x25, 0x0F,        //     Logical Maximum (15)
	  0x75, 0x06,        //     Report Size (6)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
	  0x09, 0x30,        //     Usage (X)
	  0x15, 0x00,        //     Logical Minimum (0)
	  0x26, U16_LO(TOUCHPAD_X_SIZE), U16_HI(TOUCHPAD_X_SIZE),  //     Logical Maximum (3684)
	  0x35, 0x00,        //     Physical Minimum (0)
	  0x46, U16_LO(PHY_X), U16_HI(PHY_X),  //     Physical Maximum (401)
	  0x55, 0x0E,        //     Unit Exponent (-3)
	  0x65, 0x13,        //     Unit (System: SI Linear, Length: Centimeter)
	  0x75, 0x10,        //     Report Size (16)
	  0x95, 0x01,        //     Report Count (1)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x09, 0x31,        //     Usage (Y)
	  0x26, U16_LO(TOUCHPAD_Y_SIZE), U16_HI(TOUCHPAD_Y_SIZE),  //     Logical Maximum (2176)
	  0x46, U16_LO(PHY_Y), U16_HI(PHY_Y),  //     Physical Maximum (275)
	  0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0xC0,              //   End Collection

	  0x05, 0x0D,        //   Usage Page (Digitizer)
	  0x55, 0x0C,        //   Unit Exponent (-4)
	  0x66, 0x01, 0x10,  //   Unit (System: SI Linear, Time: Seconds)
	  0x47, 0xFF, 0xFF, 0x00, 0x00,  //   Physical Maximum (65534)
	  0x27, 0xFF, 0xFF, 0x00, 0x00,  //   Logical Maximum (65534)
	  0x75, 0x10,        //   Report Size (16)
	  0x95, 0x01,        //   Report Count (1)
	  0x09, 0x56,        //   Usage (0x56)
	  0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)

	  0x09, 0x54,        //   Usage (0x54)
	  0x25, 0x7F,        //   Logical Maximum (127)
	  0x95, 0x01,        //   Report Count (1)
	  0x75, 0x08,        //   Report Size (8)
	  0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)

	  0x05, 0x09,        //   Usage Page (Button)
	  0x09, 0x01,        //   Usage (0x01)
	  0x25, 0x01,        //   Logical Maximum (1)
	  0x75, 0x01,        //   Report Size (1)
	  0x95, 0x01,        //   Report Count (1)
	  0x81, 0x02,        //   Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
	  0x95, 0x07,        //   Report Count (7)
	  0x81, 0x03,        //   Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)


	  0xC0,  //HID_ENDCOLLECTION,
#endif
	  
};




const uint8_t accel_info[]={
//0x5A,0x5A,0x5A,0x5A,0x2A,0x00,0x90,0x00,0x0D,0x00,0x0E,0x00,
0x3A,0x05,0x01,0x1E,0x38,0x00,0x02,0x00,0xF9,0x03,0x3A,0x00,0x06,
0x00,0xFB,0x03,0x3A,0x00,0x00,0x00,0xFA,0x03,0x3B,0x00,0x07,0x00,0xFB,0x03,0x38,0x00,0x05,0x00,0xF8,0x03
//,0x6A,0x11,0x0A,0x0D
};


const uint8_t touch_data[]={0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};





void keyboard_connect()
{		
		static uint8_t send_device_info_cnt=0;
		if(handshake==0  || (handshake==1 && handshake_ack_tick>=2000))//发握手包
		{
			handshake=1;
        	handshake_ack_tick=0;
//			kb_serial=0;
//			printf("send handshake\r\n");
			uint8_t data[] = {0x3A,0x0D,0x01,0x01,0x01};
			one_wire_bus_app_send_short_keyboard_custom_data(0x80,data,5);
//			uart_100us_timer_tick=0;
		}
		else if(handshake==2  )   //发一次抬手让平板回ack  && uart_100us_timer_tick>600
		{
			handshake=3;
//			printf("handshake=%d\r\n",handshake);
//			uart_100us_timer_tick=0;

		}

		
		if(device_info_send_flag==1 ||(device_info_send_flag==2 && handshake_ack_tick>=1000))
		{
        	handshake_ack_tick=0;
//		    printf("Keyboard_Information\r\n");
		    bool ret = one_wrie_bus_app_send_device_info();
			if(ret)
			{
				device_info_send_flag=2;
			}
			send_device_info_cnt=0;
		}

		
		if(device_info_send_flag==3||(device_info_send_flag==4 && handshake_ack_tick>=1000 && send_device_info_cnt<3)) //发送hid描述符
		{
			handshake=3;
			handshake_ack_tick=0;
//			printf("send hid_reportMap\r\n");
			uart_4ms_timer_tick = 0;
			uart_4ms_timer_flag = false;
			bool ret = one_wire_bus_app_sync_reportMap((uint8_t*)hid_reportMap_aaa,sizeof(hid_reportMap_aaa));
			if(ret)
			{
				device_info_send_flag=4;
			}

			kb_serial++;
			send_device_info_cnt++;
		}

		if((device_version_info_send_flag==1 && syn_100us_timer_tick>500)/*||(device_version_info_send_flag==2 && handshake_ack_tick>5000)*/) //发送设备版本信息
		{
			handshake_ack_tick=0;
//			printf("send_device_version_info\r\n");
			syn_100us_timer_tick=0;
			bool ret = one_wrie_bus_app_send_version_info();
			if(ret)
			{
				device_version_info_send_flag=2;
			}
			
		}
//		else 
		if((device_extend_info_send_flag==1 && syn_100us_timer_tick>500)/*||(device_extend_info_send_flag==2&& handshake_ack_tick>5000)*/) //发送设备扩展信息
		{
//			printf("send_device_extend_info\r\n");
			syn_100us_timer_tick=0;
			bool ret =  one_wrie_bus_app_send_device_extend_info();
			if(ret)
			{
				device_extend_info_send_flag=2;
			}
//			printf("send_accel_info\r\n");
			accel_info_send_flag=2;		
			global_gsensor_enable=true;
		}
//		else 
		if(accel_info_send_flag == 2 && syn_100us_timer_tick>300)  // 发送accel信息
		{
			accel_info_send_flag=3;
			one_wire_bus_app_send_touch((uint8_t *)&touch_data[0],0);
			uint8_t key_buf[6]={0,0,0,0,0,0};
			one_wire_bus_app_send_keyboard(0,(uint8_t *)&key_buf[0],0);
			global_key_power_on_flag=1;
		}
		
		if(one_wire_bus_app_sleep_or_wakeup_flag)
			return;
			
			
		//if(global_weakup_flag)
		{
			if(get_kb_lock_flag==1 && syn_100us_timer_tick>200)
			{
				if(get_kb_lock_cnt>=3)
				{
					get_kb_lock_cnt=0;
					get_kb_lock_flag=2;
					kb_lock_state = 0 ;
				}else{
					get_kb_lock_flag=0;
					global_gsensor_enable=true;
				}
			}
			
		}
		if( !get_kb_lock_flag && syn_100us_timer_tick>300  && gsensor_info_send_flag)
		{
			syn_100us_timer_tick = 0;
			uint8_t ucBuffer[5]={0};
			ucBuffer[0]=0x3A;
			ucBuffer[1]=0x0F;
			ucBuffer[2]=0x01;
			ucBuffer[3]=0x01;
			ucBuffer[4]=0x01;
			if( one_wire_bus_app_custom_in_ack ( (uint8_t *)ucBuffer, 5))
			{
				if(!pad_cover_state)
					get_kb_lock_flag=1;
				gsensor_info_send_flag=false;
				get_kb_lock_cnt++;
			}
		
		}
		
		if(kb_ack_no_flag)
		{
			if(start_send_pack_test_flag==1)//标志按键按下
			{
				start_send_pack_test_flag=0;
			}
		}
		if(rec_ack_flag)
		{
			rec_ack_flag=0;
			if(device_info_send_flag==2)  //发送完设备描述信息后发一次抬起收到ack后发送hid描述符   && handshake==2
			{
				device_info_send_flag=3;
			}else if(device_info_send_flag==4){
				device_info_send_flag = 5;
				
			}
				 
			if(device_version_info_send_flag==2) //设备版本信息发送
			{
				device_version_info_send_flag=3;
				
			}
			if (device_extend_info_send_flag==2 && accel_info_send_flag==0)   //设备扩展信息发送100ms后发送accel信息
			{
				device_extend_info_send_flag=3;
				syn_100us_timer_tick=0;
				accel_info_send_flag=1;
			}
			
			if(start_send_pack_test_flag==1)//标志按键按下
			{
				start_send_pack_test_flag=2;
			}
		}
}
















